//
//  CultureViewController.m
//  Blank
//
//  Created by 周星宇 on 15/9/23.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "CultureViewController.h"
#import "AFNetWorkingGet.h"
#import "CultureModel.h"
#import "CultureCell.h"
#import "CultureDetailViewController.h"
#import <MJRefresh.h>

@interface CultureViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *cultureArray;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation CultureViewController


- (void)dataHanle{
    self.cultureArray = [NSMutableArray array];
    [AFNetWorkingGet GETWithAFNByURL:MoreCultureURL completion:^(id result) {
        NSArray *array = [result objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            CultureModel *cultureModel = [CultureModel CultureModelWithDic:dic];
            [self.cultureArray addObject:cultureModel];
        }
        if (self.cultureArray.count > 0) {
            [self.tableView reloadData];
        }
    }];
}

- (void)dataHeaderFooter{
    self.tableView.header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        NSMutableArray *arr = [NSMutableArray array];
        [AFNetWorkingGet GETWithAFNByURL:MoreCultureURL completion:^(id result) {
            NSArray *array = [result objectForKey:@"data"];
            for (NSDictionary *dic in array) {
                CultureModel *cultureModel = [CultureModel CultureModelWithDic:dic];
                [arr addObject:cultureModel];
            }
            if (self.cultureArray.count > 0) {
                [self.cultureArray removeAllObjects];
                self.cultureArray = arr;
                [self.tableView reloadData];
                [self.tableView.header endRefreshing];
            }
            else{
                NSLog(@"没有数据");
            }
        }];
    }];
    __block NSInteger i = 2;
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        NSString *url = [NSString stringWithFormat:@"%@%ld", MoreCultureUpURL, i];
        i++;
        [AFNetWorkingGet GETWithAFNByURL:url completion:^(id result) {
            NSArray *array = [result objectForKey:@"data"];
            for (NSDictionary *dic in array) {
                CultureModel *cultureModel = [CultureModel CultureModelWithDic:dic];
                [self.cultureArray addObject:cultureModel];
            }
            if (self.cultureArray.count > 0) {
                [self.tableView reloadData];
                [self.tableView.footer endRefreshing];
            }
            else{
                NSLog(@"没有数据");
            }
        }];
    }];
}

- (void)tableViewSubViews{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 45 * Height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = 0;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[CultureCell class] forCellReuseIdentifier:@"culture"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"文化传播";
    // Do any additional setup after loading the view.
    [AFNetWorkingGet popAlertIfNoInter:self];
    [self withNavigationItem];
    [self dataHanle];
    [self tableViewSubViews];
    [self dataHeaderFooter];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNightModel) name:@"nightModelChange" object:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.cultureArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150 * Height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1 * Height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1 * Height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CultureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"culture"];
    CultureModel *cultureModel = [self.cultureArray objectAtIndex:indexPath.section];
    cell.cultureModel = cultureModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CultureDetailViewController *culture = [[CultureDetailViewController alloc] init];
    CultureModel *cultureModel = [self.cultureArray objectAtIndex:indexPath.section];
    culture.cid = cultureModel.cid;
    culture.name = cultureModel.subject;
    [self.navigationController pushViewController:culture animated:YES];
}

- (void)withNavigationItem{
    UIButton *leftMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftMenuBtn.frame = CGRectMake(0, 0, 25 * Windth, 25 * Height);
    [leftMenuBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [leftMenuBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftMenuBtn];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) changeNightModel
{
    if ([NightModel getColorModel]) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:57.0f/255 green:79.0f/255 blue:86.0f/255 alpha:1];
        self.tableView.sectionIndexColor = [UIColor greenColor];
        [self.tableView reloadData];
    }
    else{
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.sectionIndexBackgroundColor = nil;
        self.tableView.sectionIndexColor =nil;
        [self.tableView reloadData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
